package cn.bgs.service.impl;

import cn.bgs.pojo.VehicleArea;
import cn.bgs.feign.FileFeign;
import cn.bgs.common.R;
import cn.bgs.pageUtil.MybatisPageHelper;
import cn.bgs.pageUtil.PageResult;
import cn.bgs.pojo.VehicleCar;
import cn.bgs.mapper.VehicleCarMapper;
import cn.bgs.pojo.vo.VehicleCarVo;
import cn.bgs.service.IVehicleCarService;
import cn.bgs.vo.VehicleAdd;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.bgs.vo.UserVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.List;

import java.util.List;

import java.util.List;

/**
 * <p>
 * 车辆表 服务实现类
 * </p>
 *
 * @author cf
 * @since 2022-06-07
 */
@Service
public class VehicleCarServiceImpl extends ServiceImpl<VehicleCarMapper, VehicleCar> implements IVehicleCarService {
    @Autowired
    VehicleCarMapper vehicleCarMapper;
    @Autowired
    private FileFeign fileFeign;
    @Override
    public R selectCarInfo(VehicleCar vehicleCar) {
        MybatisPageHelper.startPage(vehicleCar.getPageRequest());
        PageResult pageResult = MybatisPageHelper.getPageResult(vehicleCarMapper.selectCarInfo(vehicleCar));
        return R.ok().data("data",pageResult);
    }

    @Override
    public R deleteInfo(Integer vehicleCarId) {
        int delete = vehicleCarMapper.deleteInfo(vehicleCarId);
        return R.ok().data("delete", delete);
    }




    @Override
    public List<VehicleCar> getMyCar(String userId) {
        List<VehicleCar> vehicleCarList = vehicleCarMapper.getMyCar(userId);
        return vehicleCarList;
    }

    @Override
    public String addMyCar(VehicleCarVo vehicleCarVo) {
        int id = vehicleCarMapper.addMyCar(vehicleCarVo);
        fileFeign.addCarBody(vehicleCarVo.getVehicleCarId());
        return String.valueOf(id);
    }

    Logger log= LogManager.getLogger();


    @Override
    public List<VehicleCar> appFindVehicleCar(Integer userId) {

        String vehicleState="未绑定";
        QueryWrapper<VehicleCar> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id",userId);
        wrapper.eq("vehicle_state",vehicleState);
        return list(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void appudData(VehicleAdd vehicleAdd) {

        try {
            LocalDateTime localDateTime = LocalDateTime.now();

            vehicleAdd.setVehicleState("已绑定");
            vehicleAdd.setAuditState("认证中");
            vehicleAdd.setCreateTime(localDateTime);
            vehicleAdd.setUpdateTime(localDateTime);
            vehicleAdd.setDeleteFlag("n");
            vehicleCarMapper.updateVehicleCar(vehicleAdd);
            vehicleCarMapper.updateVehicleNumber(vehicleAdd);
            vehicleCarMapper.addVehicleMiddle(vehicleAdd);

            vehicleAdd.setVisitState("已失效");
            VehicleCar vehicleCar=  vehicleCarMapper.findMonthCard(vehicleAdd);
            if (vehicleCar.getMonthCardState().equals("是")){
                vehicleAdd.setPayFees(0.0);
                vehicleAdd.setPayFeesState("已缴费");
                vehicleCarMapper.addTemporaryCarPark(vehicleAdd);
            }else {
                vehicleAdd.setPayFees(10*vehicleAdd.getDuration());
                vehicleAdd.setPayFeesState("未缴费");
                vehicleCarMapper.addTemporaryCarPark(vehicleAdd);
            }

        }catch (Exception e){
            log.error(e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

    }

    @Override
    public VehicleCar getOneCar(String carNumber) {
        VehicleCar car = vehicleCarMapper.getOneCar(carNumber);
        return car;
    }

    @Override
    public int updCar(VehicleCar vehicleCar) {
        return vehicleCarMapper.updCar(vehicleCar);
    }

    @Override
    public List<VehicleCar> getAllCar() {
        return vehicleCarMapper.getAllCar();
    }
    @Override
    public boolean addVcarInfo(VehicleCar vehicleCar) {
        boolean a = vehicleCarMapper.addVcarInfo(vehicleCar);
        return a;
    }

    @Override
    public boolean updateInfo(VehicleCar vehicleCar) {
        int i = vehicleCarMapper.updateInfo(vehicleCar);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public R infoPc(UserVo userVo) {
        List<VehicleCar> list = vehicleCarMapper.infoPc(userVo);
        return R.ok().data("data",list);
    }
}
